package cn.edu.henu.team.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import org.song.videoplayer.DemoQSVideoView;
import org.song.videoplayer.IVideoPlayer;
import org.song.videoplayer.PlayListener;
import org.song.videoplayer.QSVideo;

import java.util.ArrayList;

import cn.edu.henu.team.R;
import cn.edu.henu.team.bean.video;

public class VideoAdapter extends BaseAdapter {
   private ArrayList<video> mData = new ArrayList<video>();
   private Context mContext;


   public VideoAdapter(ArrayList<video> mData, Context mContext) {
      this.mData = mData;
      this.mContext = mContext;
   }

   public VideoAdapter() {
   }

   @Override
   public int getCount() {
      return mData.size();
   }

   @Override
   public Object getItem(int position) {
      return mData.get(position);
   }

   @Override
   public long getItemId(int position) {
      return position;
   }

   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
      if(convertView == null) {
         convertView = LayoutInflater.from(mContext).inflate(R.layout.video_item, null);
      }
      DemoQSVideoView qsVideoView = (DemoQSVideoView) convertView.findViewById(R.id.myVideoView);
      String url = mData.get(position).getUrl();
      qsVideoView.setUp(url, mData.get(position).getName());
      //设置多个清晰度
      qsVideoView.setUp(
              QSVideo.Build(url).title(mData.get(position).getName()).definition("超清").resolution("超清 720P").build());
      RequestOptions requestOptions = new RequestOptions()
              .placeholder(R.drawable.loading)
              .error(R.drawable.loading)
              .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
      Glide.with(mContext).load(mData.get(position).getCover()).apply(requestOptions).centerCrop().into(qsVideoView.getCoverImageView());
      //设置监听


      qsVideoView.setPlayListener(new PlayListener() {
         @Override
         public void onStatus(int status) {//播放器的ui状态
            if (status == IVideoPlayer.STATE_AUTO_COMPLETE)
               qsVideoView.quitWindowFullscreen();//播放完成退出全屏
         }

         @Override//全屏/普通/浮窗...
         public void onMode(int mode) {

         }

         @Override//播放事件 初始化完成/缓冲/出错/点击事件...
         public void onEvent(int what, Integer... extra) {

         }


      });
      //进入全屏的模式 0横屏 1竖屏 2传感器自动横竖屏 3根据视频比例自动确定横竖屏      -1什么都不做
      qsVideoView.enterFullMode=3;
      return convertView;
   }
}
